#include <iostream>
#include <vector>
#include <string>
#include <unordered_map>
#include <thread>
#include <chrono>
#include "util.hpp"

#include <sw/redis++/redis++.h>
using namespace std::chrono_literals;

using std::cout;
using std::endl;
using std::string;
using std::unordered_map;
using std::vector;

using sw::redis::Redis;

void test1(Redis &redis){
    std::cout<<"zadd 和 zrange"<<std::endl;
    redis.flushall();

    redis.zadd("key","吕布",99);
    redis.zadd("key",{
        std::make_pair("赵云",98),
        std::make_pair("典韦",97)
    });
    vector<std::pair<string,double>> members = {
        std::make_pair("关羽",96),
        std::make_pair("张飞",95)
    };
    redis.zadd("key",members.begin(),members.end());

    //zrange支持两种主要的风格；
    //1.只查询member，不带score
    //2.查询member同时带score
    //关键就是看插入迭代器指向的容器的类型
    //指向的容器只是包含一个string，就是只查询member
    //指向的容器包含的是一个pair，里面有string和double，就是查询member，同时带有score
    vector<string> membersResults;
    auto it = std::back_inserter(membersResults);
    redis.zrange("key",0,-1,it);
    printContainer(membersResults);

    vector<std::pair<string,double>> membersWithScore;
    auto it2 = std::back_inserter(membersWithScore);
    redis.zrange("key",0,-1,it2);
    printContanerPair(membersWithScore);
}
void test2(Redis &redis){
    std::cout<<"zcard"<<std::endl;
    redis.flushall();

    redis.zadd("key","zhangsan",90);
    redis.zadd("key","lisi",91);
    redis.zadd("key","wangwu",92);
    redis.zadd("key","zhaoliu",93);

    long long result = redis.zcard("key");
    std::cout<<"result:"<<result<<std::endl;
}
void test3(Redis &redis){
    std::cout<<"zrem"<<std::endl;
    redis.flushall();

    redis.zadd("key","zhangsan",90);
    redis.zadd("key","lisi",91);
    redis.zadd("key","wangwu",92);
    redis.zadd("key","zhaoliu",93);

    redis.zrem("key","zhangsan");

    long long result = redis.zcard("key");
    std::cout<<"result:"<<result<<std::endl;
}
void test4(Redis &redis){
    std::cout<<"zscore"<<std::endl;
    redis.flushall();

    redis.zadd("key","zhangsan",90);
    redis.zadd("key","lisi",91);
    redis.zadd("key","wangwu",92);
    redis.zadd("key","zhaoliu",93);

    auto score = redis.zscore("key","zhangsan");
    if(score){
        std::cout<<"score:"<<score.value()<<std::endl;
    }else{
        std::cout<<"score无效"<<std::endl;
    }
}
void test5(Redis &redis){
    std::cout<<"zrank"<<std::endl;
    redis.flushall();

    redis.zadd("key","zhangsan",90);
    redis.zadd("key","lisi",91);
    redis.zadd("key","wangwu",92);
    redis.zadd("key","zhaoliu",93);

    auto rank = redis.zrank("key","zhangsan");
    if(rank){
        std::cout<<"rank:"<<rank.value()<<std::endl;
    }else{
        std::cout<<"rank无效"<<std::endl;
    }
}  
int main(){
    Redis redis("tcp://127.0.0.1:6379");
    //test1(redis);
    //test2(redis);
    //test3(redis);
    //test4(redis);
    test5(redis);
    return 0;
}